/*     */ package com.cr.quartz.service.impl;
/*     */ 
/*     */ import com.cr.common.constant.ScheduleConstants;
/*     */ import com.cr.common.core.text.Convert;
/*     */ import com.cr.common.exception.job.TaskException;
/*     */ import com.cr.quartz.domain.SysJob;
/*     */ import com.cr.quartz.mapper.SysJobMapper;
/*     */ import com.cr.quartz.service.ISysJobService;
/*     */ import com.cr.quartz.util.CronUtils;
/*     */ import com.cr.quartz.util.ScheduleUtils;
/*     */ import java.util.List;
/*     */ import javax.annotation.PostConstruct;
/*     */ import org.quartz.JobDataMap;
/*     */ import org.quartz.JobKey;
/*     */ import org.quartz.Scheduler;
/*     */ import org.quartz.SchedulerException;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ public class SysJobServiceImpl implements ISysJobService {
/*     */   @Autowired
/*     */   private Scheduler scheduler;
/*     */   
/*     */   @Autowired
/*     */   private SysJobMapper jobMapper;
/*     */   
/*     */   @PostConstruct
/*     */   public void init() throws SchedulerException, TaskException {
/*  42 */     this.scheduler.clear();
/*  43 */     List<SysJob> jobList = this.jobMapper.selectJobAll();
/*  44 */     for (SysJob job : jobList)
/*  46 */       ScheduleUtils.createScheduleJob(this.scheduler, job); 
/*     */   }
/*     */   
/*     */   public List<SysJob> selectJobList(SysJob job) {
/*  59 */     return this.jobMapper.selectJobList(job);
/*     */   }
/*     */   
/*     */   public SysJob selectJobById(Long jobId) {
/*  71 */     return this.jobMapper.selectJobById(jobId);
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public int pauseJob(SysJob job) throws SchedulerException {
/*  83 */     Long jobId = job.getJobId();
/*  84 */     String jobGroup = job.getJobGroup();
/*  85 */     job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
/*  86 */     int rows = this.jobMapper.updateJob(job);
/*  87 */     if (rows > 0)
/*  89 */       this.scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup)); 
/*  91 */     return rows;
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public int resumeJob(SysJob job) throws SchedulerException {
/* 103 */     Long jobId = job.getJobId();
/* 104 */     String jobGroup = job.getJobGroup();
/* 105 */     job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
/* 106 */     int rows = this.jobMapper.updateJob(job);
/* 107 */     if (rows > 0)
/* 109 */       this.scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup)); 
/* 111 */     return rows;
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public int deleteJob(SysJob job) throws SchedulerException {
/* 123 */     Long jobId = job.getJobId();
/* 124 */     String jobGroup = job.getJobGroup();
/* 125 */     int rows = this.jobMapper.deleteJobById(jobId);
/* 126 */     if (rows > 0)
/* 128 */       this.scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup)); 
/* 130 */     return rows;
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public void deleteJobByIds(String ids) throws SchedulerException {
/* 143 */     Long[] jobIds = Convert.toLongArray(ids);
/* 144 */     for (Long jobId : jobIds) {
/* 146 */       SysJob job = this.jobMapper.selectJobById(jobId);
/* 147 */       deleteJob(job);
/*     */     } 
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public int changeStatus(SysJob job) throws SchedulerException {
/* 160 */     int rows = 0;
/* 161 */     String status = job.getStatus();
/* 162 */     if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
/* 164 */       rows = resumeJob(job);
/* 166 */     } else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
/* 168 */       rows = pauseJob(job);
/*     */     } 
/* 170 */     return rows;
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public void run(SysJob job) throws SchedulerException {
/* 182 */     Long jobId = job.getJobId();
/* 183 */     SysJob tmpObj = selectJobById(job.getJobId());
/* 185 */     JobDataMap dataMap = new JobDataMap();
/* 186 */     dataMap.put("TASK_PROPERTIES", tmpObj);
/* 187 */     this.scheduler.triggerJob(ScheduleUtils.getJobKey(jobId, tmpObj.getJobGroup()), dataMap);
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public int insertJob(SysJob job) throws SchedulerException, TaskException {
/* 199 */     job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
/* 200 */     int rows = this.jobMapper.insertJob(job);
/* 201 */     if (rows > 0)
/* 203 */       ScheduleUtils.createScheduleJob(this.scheduler, job); 
/* 205 */     return rows;
/*     */   }
/*     */   
/*     */   @Transactional(rollbackFor = {Exception.class})
/*     */   public int updateJob(SysJob job) throws SchedulerException, TaskException {
/* 217 */     SysJob properties = selectJobById(job.getJobId());
/* 218 */     int rows = this.jobMapper.updateJob(job);
/* 219 */     if (rows > 0)
/* 221 */       updateSchedulerJob(job, properties.getJobGroup()); 
/* 223 */     return rows;
/*     */   }
/*     */   
/*     */   public void updateSchedulerJob(SysJob job, String jobGroup) throws SchedulerException, TaskException {
/* 234 */     Long jobId = job.getJobId();
/* 236 */     JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup);
/* 237 */     if (this.scheduler.checkExists(jobKey))
/* 240 */       this.scheduler.deleteJob(jobKey); 
/* 242 */     ScheduleUtils.createScheduleJob(this.scheduler, job);
/*     */   }
/*     */   
/*     */   public boolean checkCronExpressionIsValid(String cronExpression) {
/* 254 */     return CronUtils.isValid(cronExpression);
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-quartz-4.7.0.jar!\com\cr\quartz\service\impl\SysJobServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */